package com.yyfax.pay.third.facade.fuioudeposit;

import java.io.IOException;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.CharEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Service;

import com.yyfax.framework.common.Log;

/**
 * 富友错误码服务
 * 
 * @author heizq
 * @date 2017-09-20 11:21:41
 * @since v1.0.0
 */
@Service
public class FuiouErrorCodeService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	private Properties props;

	@PostConstruct
	private void init() throws IOException {
		final String op = "FuiouErrorCodeService.init";
		logger.info(Log.op(op).msg("开始加载fuiouErrorCode.properties文件内容.").toString());
		props = new Properties();
		try {
			// 使用spring resource加载文件
			Resource resource = new ClassPathResource("fuiouErrorCode.properties");
			EncodedResource encodedResource = new EncodedResource(resource, CharEncoding.UTF_8);
			props = PropertiesLoaderUtils.loadProperties(encodedResource);
		} catch (IOException e) {
			logger.error(Log.op(op).msg("加载fuiouErrorCode.properties文件出现IOException").toString(), e);
			throw e;
		}
		logger.info(Log.op(op).msg("加载fuiouErrorCode.properties文件内容完成.").toString());
	}

	/**
	 * 根据key获取值
	 * 
	 * @author heizq
	 * @date 2017-09-20 11:28:33
	 * @since v1.0.0
	 * @param key
	 * @return
	 */
	public String getProperty(String key) {
		return props.getProperty(key);
	}

	/**
	 * 根据key获取值，没有返回指定的默认值
	 * 
	 * @author heizq
	 * @date 2017-09-20 11:28:33
	 * @since v1.0.0
	 * @param key
	 * @return
	 */
	public String getProperty(String key, String defaultValue) {
		return props.getProperty(key, defaultValue);
	}
}
